home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / gcl-1.0-l / gcl-1 / usr / local / lib / gcl-1.0 / doc / gcl.el < prev    next >
Encoding:
Text File  |  1994-05-10  |  10.9 KB  |  346 lines

  1. ;; Copyright  William F. Schelter.   1994
  2. ;; Licensed by GNU public license.
  3.  
  4. ;; You should copy find-doc.el, gcl.el, lisp-complete.el to the emacs/lisp directory.
  5.  
  6. ;; Some commands and macros for dealing with lisp
  7. ;; M-X run : run gcl or another lisp
  8. ;; m-c-x ; evaluate defun in the other window or in the last lisp which you were using.
  9. ;; m-c-x ; with a numeric arg : compile the current defun in the other window
  10. ;; m-c-d ; disassemble in other window.
  11. ;; M-x macroexpand-next : macro expand the next sexp in other window.
  12. ;; C-h d Find documentation on symbol where the cursor is.
  13. ;; C-h / Find documentation on all strings containing a given string.
  14. ;; M-p complete the current input by looking back through the buffer to see what was last typed
  15. ;;        using this prompt and this beginning.   Useful in shell, in lisp, in gdb,...
  16.  
  17.  
  18. (setq lisp-mode-hook  'remote-lisp)
  19.  
  20. (autoload 'visit-doc-file "find-doc" nil t)
  21.  
  22. (autoload 'find-doc "find-doc" "Display documentation about STRING" t)
  23.  
  24. (autoload 'lisp-complete "lisp-complete" nil t)
  25.  
  26. (global-set-key "p" 'lisp-complete)
  27. (global-set-key "d" 'find-doc)
  28.  
  29. (defun remote-lisp (&rest l)
  30.   (and (boundp 'lisp-mode-map)
  31.        lisp-mode-map
  32.   (define-key lisp-mode-map "\e\C-d" 'lisp-send-disassemble)
  33.   (define-key lisp-mode-map "\e\C-x" 'lisp-send-defun-compile)
  34.   (make-local-variable 'lisp-package)
  35.   (setq lisp-package nil)
  36.   (and (boundp 'remote-lisp-hook) (funcall remote-lisp-hook))
  37.   ))
  38.  
  39.  
  40. (defvar search-back-for-lisp-package-p nil)
  41.  
  42. ;; look at the beginning of buffer to try to find an in package statement
  43. (defun get-buffer-package ()
  44.  
  45.   "Returns what it thinks is the lisp package for the current buffer.
  46. It caches this information in the local variable `lisp-package'.  It
  47. obtains the information from searching for the first in-package from
  48. the beginning of the file.  Since in common lisp, there is only
  49. supposed to be one such statement, it should be able to determine
  50. this.  By setting lisp-package to t, you may disable its search.  This
  51. will also disable the automatic inclusion of an in-package statement
  52. in the tmp-lisp-file, used for sending forms to the current
  53. lisp-process."
  54.  
  55.   (cond ((eq lisp-package t) nil)
  56.     (search-back-for-lisp-package-p
  57.      (save-excursion
  58.        (cond ((re-search-backward "^[ \t]*(in-package "  nil t)
  59.           (goto-char (match-end 0))
  60.           (read (current-buffer))))))
  61.     (lisp-package lisp-package)
  62.     (t
  63.      (setq
  64.       lisp-package
  65.       (let (found success)
  66.         (save-excursion
  67.           (goto-char (point-min))
  68.           (while (not found)
  69.         (if (and (setq success (search-forward "(in-package " 1000 t))
  70.              (not (save-excursion
  71.                 (beginning-of-line)
  72.                 (looking-at "[ \t]*;"))))
  73.             (setq found  (read (current-buffer))))
  74.         (if (>= (point) 980) (setq found t))
  75.         (or success (setq found t))
  76.         ))
  77.         found)))))
  78.  
  79.  
  80. (defun run (arg)
  81.   "Run an inferior Lisp process, input and output via buffer *lisp*."
  82.   (interactive "sEnter name of file to run: ")
  83.   (require 'shell)
  84.   (setq lisp-mode-hook 'remote-lisp)
  85.   (switch-to-buffer  (make-shell (concat arg "-lisp") arg nil "-i"))
  86.   (make-local-variable 'shell-prompt-pattern)
  87.     (setq shell-prompt-pattern "^[^#%)>]*[#%)>]+ *")
  88.     (cond ((or (string-match "maxima" arg) (string-match "affine" arg)
  89.        (save-excursion     (sleep-for 2)
  90.                    (re-search-backward "maxima"
  91.                            (max 1 (- (point) 300))
  92.                            t)))
  93.        (require 'maxima-mode)
  94.        (inferior-maxima-mode)
  95.        (goto-char (point-max))
  96.        )
  97.       (t  (inferior-lisp-mode))))
  98.  
  99. (defun lisp-send-disassemble (arg)
  100.   (interactive "P")
  101.    (if  arg 
  102.        ( lisp-send-defun-compile "disassemble-h")
  103.             ( lisp-send-defun-compile "disassemble"))
  104.      )
  105.  
  106. (defvar time-to-throw-away nil)
  107. (defvar telnet-new-line "")
  108.  
  109. (defun lisp-send-defun-compile (arg)
  110.  
  111.   "Send the current defun (or other form) to the lisp-process.  If there
  112. is a numeric arg, the form (compile function-name) is also sent.  The
  113. value of lisp-process will be the process of the other exposed window (if
  114. there is one) or else the global value of lisp-process.  If the
  115. ...received message is not received, probably either the reading of
  116. the form caused an error.   If the process does not have telnet in
  117. its name, then we write a tmp file and load it.
  118. If :sdebug is in *features*, then si::nload is used instead of
  119. ordinary load, in order to record line information for debugging.
  120.  
  121. The value of `lisp-package' if non nil, will be used in putting an
  122. in-package statement at the front of the tmp file to be loaded.
  123. `lisp-package' is determined automatically on a per file basis,
  124. by get-buffer-package.
  125. "
  126.  
  127.   (interactive "P")
  128.   (other-window 1)
  129.   (let* ((proc (or (get-buffer-process (current-buffer)) lisp-process))
  130.      def beg
  131.      (this-lisp-process proc)
  132.      (lisp-buffer (process-buffer this-lisp-process))
  133.      fun)
  134.     (other-window 1)
  135.     (save-excursion
  136.       (end-of-defun)
  137.       (let ((end (dot)) (buffer (current-buffer))
  138.         (proc (get-process this-lisp-process)))
  139.     (setq lisp-process proc)
  140.     (beginning-of-defun)
  141.     (save-excursion
  142.       (cond ((and arg (looking-at "(def")) (setq def t))
  143.         (t (setq arg nil)))
  144.       (cond (def (forward-char 2)(forward-sexp 1)
  145.              (setq fun (read buffer))
  146.              (setq fun (prin1-to-string fun))
  147.              (message (format
  148.                    "For the lisp-process %s: %s"
  149.                    (prin1-to-string this-lisp-process) fun)))))
  150.     (cond ((eql (char-after (1- end)) ?\n)
  151.            (setq end (1- end)) ))
  152.     (setq beg (dot))
  153.     (my-send-region this-lisp-process beg end)
  154.     ))
  155.     
  156.  
  157.     (send-string this-lisp-process
  158.          (concat ";;end of form" "\n" telnet-new-line))
  159.     (cond (arg
  160.        (if (numberp arg) (setq arg "compile"))
  161.        (send-string this-lisp-process (concat "(" arg "'" fun ")"
  162.                           telnet-new-line))))
  163.     (and time-to-throw-away
  164.      (string-match "telnet"(buffer-name (process-buffer proc)))
  165.      (dump-output proc time-to-throw-away))
  166.     (cond (nil  (get-buffer-window lisp-buffer)
  167.           (select-window (get-buffer-window lisp-buffer))
  168.           (goto-char (point-max)))
  169.       (t nil))))
  170.  
  171. (defvar telnet-new-line "")
  172. (defvar tmp-lisp-file (concat "/tmp/" (user-login-name) ".lsp"))
  173.  
  174. (defun my-send-region (proc beg end)
  175.   (cond ((or (string-match "telnet" (process-name proc)))
  176.      (send-region proc beg end))
  177.     (t
  178.      (let ((package (get-buffer-package)))
  179.        (save-excursion
  180.          (let ((temp-buffer-show-hook '(lambda (x) nil)))
  181.            (with-output-to-temp-buffer  "*tmp-gcl*"
  182.          (if package
  183.              (prin1 (list 'in-package  package)))
  184.          (princ ";!(:line ")
  185.          (prin1
  186.            (let ((na (buffer-file-name (current-buffer))))
  187.              (if na (expand-file-name na)
  188.                (buffer-name (current-buffer))))
  189.            )
  190.          (princ (- (count-lines (point-min) (+ beg 5)) 1))
  191.          (princ ")\n")
  192.          (set-buffer "*tmp-gcl*")
  193.          (write-region (point-min) (point-max) tmp-lisp-file nil nil))))
  194.        (write-region beg end tmp-lisp-file t nil)
  195.        (message "sending ..")
  196.        (send-string
  197.         proc
  198.         (concat "(lisp::let ((*load-verbose* nil)) (#+sdebug si::nload #-sdebug load \""
  199.             tmp-lisp-file
  200.             "\")#+gcl(setq si::*no-prompt* t)(values))\n  ")
  201.             )
  202.        (message (format "PACKAGE: %s ..done" (or package "none")))
  203.  
  204.        ))))
  205.  
  206. (defun dump-output (proc seconds)
  207.   "dump output for PROCESS for SECONDS or to \";;end of form\""
  208.  (let ((prev-filter (process-filter proc)) (already-waited 0))
  209.        (unwind-protect (progn (set-process-filter proc 'dump-filter)
  210.                   (while (< already-waited seconds)
  211.                   (sleep-for 1)(setq already-waited
  212.                          (1+ already-waited))))
  213.      (set-process-filter proc prev-filter))))
  214.  
  215.  
  216.  
  217. (defun dump-filter (proc string)
  218. ;  (setq she (cons string she))
  219.   (let ((ind (string-match ";;end of form" string)))
  220.     (cond (ind (setq string (substring
  221.                  string
  222.                  (+ ind (length
  223.                      ";;end of form"))))
  224.  
  225.            (message "... received.")
  226.            (setq already-waited 1000)
  227.            (set-process-filter proc prev-filter)
  228.            (cond (prev-filter (funcall prev-filter proc string))
  229.              (t string)))
  230.       (t ""))))
  231.  
  232.  
  233. ;;(process-filter (get-process "lisp"))
  234. (defun macroexpand-next ()
  235.   "macroexpand current form"
  236.   (interactive)
  237.   (save-excursion
  238.     (let ((beg (point)))
  239.       (forward-sexp )
  240.       (message "sending macro")
  241.  
  242.       (let* ((current-lisp-process
  243.           (or (get-buffer-process (current-buffer))
  244.                (prog2 (other-window 1)
  245.                   (get-buffer-process (current-buffer))
  246.                   (other-window 1)))))
  247.     (send-string current-lisp-process "(macroexpand '")
  248.     (send-region current-lisp-process  beg (point) )
  249.     (send-string current-lisp-process ")\n")))))
  250.  
  251. (defun delete-comment-char (arg) 
  252.   (while (and (> arg 0) (looking-at comment-start)) (delete-char 1) 
  253.      (setq arg (1- arg))))
  254.  
  255. (defun mark-long-comment ()
  256.   (interactive)
  257.   (let ((at (point)))
  258.     (beginning-of-line)
  259.     (while(and (not (eobp))
  260.            (or  (looking-at comment-start)
  261.             ;(looking-at "[     ]*\n")
  262.             ))
  263.       (forward-line 1))
  264.     (set-mark (point))
  265.     (goto-char at)
  266.     (while(and (not (bobp))
  267.            (or  (looking-at comment-start)
  268.             ;(looking-at "[     ]*\n")
  269.             ))
  270.       (forward-line -1))
  271.     (or (bobp )(forward-line 1))))
  272.     
  273.  
  274. (defun fill-long-comment ()
  275.   (interactive)
  276.   (mark-long-comment)
  277.   (let ((beg (min (dot) (mark)))
  278.     (end (max (dot) (mark))) (n 0)m)
  279.     (narrow-to-region beg end)
  280.     (goto-char (point-min))    
  281.     (while (looking-at ";")
  282.       (forward-char 1))
  283.     (setq n (- (point) beg))
  284.     (goto-char (point-min))    
  285.     (while (not (eobp))
  286.       (setq m n)
  287.       (while (> m  0)
  288.     (cond ((looking-at ";")
  289.            (delete-char 1)
  290.            (cond ((looking-at " ")(delete-char 1)(setq m 0)))
  291.            (setq m (- m 1)))
  292.           (t (setq m 0))))
  293.       (forward-line 1))
  294.     (fill-region (dot-min) (dot-max))
  295.     (goto-char (point-min))
  296.     (while (not (eobp))
  297.       (cond ((looking-at "\n")
  298.          nil)
  299.         (t(insert ";; ")))
  300.       (forward-line 1))
  301.    (goto-char (point-min))
  302.    (set-mark (point-max))
  303.    (widen)))
  304.  
  305. (defun comment-region (arg) 
  306.   "Comments the region, with a numeric arg deletes up to arg comment 
  307. characters from the beginning of each line in the region.  The region stays, 
  308. so a second comment-region adds another comment character" 
  309.  (interactive "P") 
  310.  (save-excursion 
  311.    (let ((beg (dot)) 
  312.      (ok t)(end (mark)))
  313.           (comment-region1 beg end arg))))
  314.  
  315. (defun comment-region1 (beg end arg)
  316.   (let ((ok t))
  317.     (cond((> beg end) 
  318.       (let ((oth end)) 
  319.         (setq end beg beg oth)))) 
  320.     (narrow-to-region beg end) 
  321.     (goto-char beg) 
  322.        (unwind-protect 
  323.        (while ok 
  324.          (cond (arg 
  325.             (delete-comment-char arg)) 
  326.            (t   (insert-string comment-start)))
  327.          (if (< end (dot)) (setq ok nil)
  328.            (if  (search-forward "\n" end t) nil (setq ok nil))) )
  329.      (widen))))
  330.  
  331. (defun trace-expression ()
  332.   (interactive)
  333.   (save-excursion
  334.      (forward-sexp )
  335.     (let ((end (point)))
  336.            (forward-sexp -1)
  337.       (other-window 1)
  338.       (let* ((proc (get-buffer-process (current-buffer)))
  339.          (current-lisp-process (or  proc lisp-process)))
  340.     (other-window 1)
  341.     (message "Tracing: %s" (buffer-substring (point) end))
  342.     (send-string current-lisp-process "(trace ")
  343.     (send-region current-lisp-process (point) end)
  344.     (send-string current-lisp-process ")\n")))))
  345.  
  346. (provide 'gcl)